<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { transportAdd } from '@/api/common'
import { transportType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())
  write?.getRlus().value?.validate(async (valid, fields) => {
    if (valid) {
      const data = {
        name: write?.getFormData()?.name,
        min: write?.getFormData()?.min,
        max: write?.getFormData()?.max,
        predict: write?.getFormData()?.predict,
        customs_min: write?.getFormData()?.custom_min,
        customs_max: write?.getFormData()?.custom_max,
        endplace_min: write?.getFormData()?.endplace_min,
        endplace_max: write?.getFormData()?.endplace_max,
        density: write?.getFormData()?.densityList
      } as transportType

      console.log('---采购单新增参数---', data)
      const res = await transportAdd(data)
      if (res) {
        emitter.emit('getList', 'add')
        push('/sysParameter/transport')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/sysParameter/transport')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
